/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package almacen;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Tesla
 */
public final class editProducto extends javax.swing.JFrame {

    Connection cn;
    PreparedStatement ps;
    ResultSet rs;
    static DefaultListModel model2 = new DefaultListModel();
    boolean hayImagen=false;
    int codigo;
    int cproveedor;

    public editProducto() {

        initComponents();
        id_producto.setEnabled(false);
        cantidadalmacen.setDocument(new LimitadorCaracteres());
        id_producto.setDocument(new LimitadorCaracteres());
        cantidadalmacen.setDocument(new LimitadorCaracteres());
        minimo_stock.setDocument(new LimitadorCaracteres());
        peso.setDocument(new LimitadorCaracteres());
        cantidad_medio.setDocument(new LimitadorCaracteres());
        cantidad_mayoreo.setDocument(new LimitadorCaracteres());
        preciocompra.setDocument(new LimitadorCaracteresFloat());
        precio_unitario.setDocument(new LimitadorCaracteresFloat());
        precio_mayoreo.setDocument(new LimitadorCaracteresFloat());
        precio_medio.setDocument(new LimitadorCaracteresFloat());
        fecha.setEnabled(false);
        jComboBox4.setSelectedIndex(1);
        jComboBox1.setSelectedIndex(2);
        activo.setSelectedIndex(1);

        nuevoProducto();




    }

    void nuevoProducto() {
        try {
            cn = local_connection.getconnect();
            ps = (PreparedStatement) cn.prepareStatement("select id_producto from productos order by id_producto desc  limit 1");
            ps.executeQuery();
            rs = ps.executeQuery();
            rs.next();
            codigo = rs.getInt("id_producto")+1;
            id_producto.setText(String.valueOf(codigo));
            model2.clear();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        cantidad_mayoreo = new javax.swing.JTextField();
        cantidad_medio = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        notas = new javax.swing.JTextArea();
        jLabel26 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        especificaciones = new javax.swing.JTextArea();
        jLabel28 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descripcion = new javax.swing.JTextArea();
        modelo = new javax.swing.JTextField();
        id_producto = new javax.swing.JTextField();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        precio_mayoreo = new javax.swing.JTextField();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel32 = new javax.swing.JLabel();
        precio_unitario = new javax.swing.JTextField();
        precio_medio = new javax.swing.JTextField();
        peso = new javax.swing.JTextField();
        jLabel33 = new javax.swing.JLabel();
        nombre = new javax.swing.JTextField();
        jLabel34 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        guardar = new javax.swing.JButton();
        minimo_stock = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        cantidadalmacen = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        ubicacionalmacen = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        activo = new javax.swing.JComboBox();
        jLabel23 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        preciocompra = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel21 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tipo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        codigo_ubicacion = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        cantidad_stock = new javax.swing.JTextField();
        categorias = new javax.swing.JButton();
        fecha = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        precio_distribuidor = new javax.swing.JTextField();
        cantidad_distribuidor = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel15 = new javax.swing.JLabel();
        imageProduct = new javax.swing.JLabel();
        imageButton = new javax.swing.JButton();
        jLabel35 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar nuevo producto");
        setIconImage(new ImageIcon(getClass().getResource("logo_rayito.png")).getImage());
        setResizable(false);

        cantidad_mayoreo.setNextFocusableComponent(precio_mayoreo);
        cantidad_mayoreo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cantidad_mayoreoActionPerformed(evt);
            }
        });

        cantidad_medio.setNextFocusableComponent(precio_medio);

        jLabel8.setText("Nombre:");

        jLabel29.setText("$");

        notas.setColumns(20);
        notas.setRows(5);
        notas.setWrapStyleWord(true);
        notas.setNextFocusableComponent(ubicacionalmacen);
        jScrollPane3.setViewportView(notas);

        jLabel26.setText("+");

        jLabel25.setText("Mayoreo: ");

        especificaciones.setColumns(20);
        especificaciones.setRows(5);
        especificaciones.setWrapStyleWord(true);
        especificaciones.setNextFocusableComponent(notas);
        jScrollPane2.setViewportView(especificaciones);

        jLabel28.setText("+ 1");

        jLabel27.setText("+");

        jScrollPane1.setNextFocusableComponent(especificaciones);

        descripcion.setColumns(20);
        descripcion.setRows(5);
        descripcion.setWrapStyleWord(true);
        jScrollPane1.setViewportView(descripcion);

        modelo.setNextFocusableComponent(descripcion);

        jLabel30.setText("$");

        jLabel31.setText("$");

        precio_mayoreo.setNextFocusableComponent(guardar);
        precio_mayoreo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                precio_mayoreoActionPerformed(evt);
            }
        });

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5%", "10%", "15%", "20%", "25%", "30%" }));
        jComboBox3.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox3PopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5%", "10%", "15%", "20%", "25%", "30%" }));
        jComboBox4.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox4PopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        jLabel32.setText("0");

        precio_unitario.setNextFocusableComponent(cantidad_medio);
        precio_unitario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                precio_unitarioActionPerformed(evt);
            }
        });
        precio_unitario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                precio_unitarioKeyReleased(evt);
            }
        });

        precio_medio.setNextFocusableComponent(cantidad_mayoreo);
        precio_medio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                precio_medioActionPerformed(evt);
            }
        });

        peso.setNextFocusableComponent(preciocompra);

        jLabel33.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel33.setText("0");

        nombre.setNextFocusableComponent(tipo);

        jLabel34.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel34.setText("0");

        jLabel11.setText("Peso:");

        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarActionPerformed(evt);
            }
        });

        minimo_stock.setNextFocusableComponent(peso);
        minimo_stock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minimo_stockActionPerformed(evt);
            }
        });

        jButton3.setText("Salir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        cantidadalmacen.setNextFocusableComponent(cantidad_stock);

        jLabel9.setText("Stock Almacen: ");

        jLabel10.setText("Minimo Stock:");

        jLabel12.setText("Ubicacion-Mostrador");

        ubicacionalmacen.setNextFocusableComponent(codigo_ubicacion);
        ubicacionalmacen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubicacionalmacenActionPerformed(evt);
            }
        });

        jLabel4.setText("Modelo:");

        jLabel5.setText("Descripcion General:");

        jLabel6.setText("Especificaciones:");

        jLabel7.setText("Notas:");

        activo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No", "Si" }));
        activo.setNextFocusableComponent(id_producto);
        activo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                activoActionPerformed(evt);
            }
        });

        jLabel23.setText("Unitario:");

        jLabel22.setText("Sugerencias:");

        jLabel24.setText("Medio-May:");

        jLabel19.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel19.setText("Precios:");

        jLabel20.setText("Precio de compra: $");

        preciocompra.setNextFocusableComponent(precio_unitario);
        preciocompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                preciocompraActionPerformed(evt);
            }
        });
        preciocompra.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                preciocompraKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                preciocompraKeyReleased(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "20%", "30%", "50%", "100%", "150%", "200%", "250%", "300%", "350%", "400%", "450%" }));
        jComboBox2.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox2PopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jLabel21.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel21.setText("Precio al Publico:");

        jLabel1.setText("Activo:");

        jLabel2.setText("Tipo:");

        tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Producto", "Servicio", "Curso" }));
        tipo.setNextFocusableComponent(modelo);
        tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoActionPerformed(evt);
            }
        });

        jLabel3.setText("Ubicacion-Almacen:");

        codigo_ubicacion.setNextFocusableComponent(cantidadalmacen);

        jLabel13.setText("Stock-Mostrador:");

        cantidad_stock.setNextFocusableComponent(minimo_stock);

        categorias.setText("Categorias");
        categorias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                categoriasActionPerformed(evt);
            }
        });

        fecha.setText("Fecha");
        fecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fechaActionPerformed(evt);
            }
        });

        jButton1.setText("Proveedor");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel14.setText("Distribuidor:");

        precio_distribuidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                precio_distribuidorActionPerformed(evt);
            }
        });

        cantidad_distribuidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cantidad_distribuidorActionPerformed(evt);
            }
        });

        jLabel16.setText("+");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5%", "10%", "15%", "20%", "25%", "30%" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel15.setText("0");

        imageProduct.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        imageButton.setText("Imagen");
        imageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imageButtonActionPerformed(evt);
            }
        });

        jLabel35.setText("$");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addGap(72, 72, 72))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(activo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(id_producto, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(75, 75, 75))
                                .addComponent(modelo)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(85, 85, 85)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addComponent(jLabel8))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(imageProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(imageButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addComponent(guardar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(categorias, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(ubicacionalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel9)
                                        .addComponent(jLabel10))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(cantidadalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(minimo_stock, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(codigo_ubicacion, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel11))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(peso, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cantidad_stock, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel20)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(preciocompra, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(4, 4, 4)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel24)
                                            .addComponent(jLabel25))
                                        .addGap(6, 6, 6)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel27)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(cantidad_medio, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel26)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(cantidad_mayoreo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(2, 2, 2)
                                        .addComponent(jLabel14)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel16)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cantidad_distribuidor, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel23)
                                            .addGap(18, 18, 18)
                                            .addComponent(jLabel28, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jLabel21)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel31)
                                        .addComponent(jLabel29)
                                        .addComponent(jLabel30))
                                    .addComponent(jLabel35, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(precio_unitario)
                                    .addComponent(precio_medio)
                                    .addComponent(precio_mayoreo)
                                    .addComponent(precio_distribuidor, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel32)
                                    .addComponent(jLabel33)
                                    .addComponent(jLabel34)
                                    .addComponent(jLabel15)))
                            .addComponent(jLabel22)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cantidadalmacen, ubicacionalmacen});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(imageButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(imageProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel12)
                                    .addComponent(codigo_ubicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel13)
                                    .addComponent(cantidad_stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addComponent(peso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fecha))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(ubicacionalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(cantidadalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jLabel10)
                                            .addComponent(minimo_stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(29, 29, 29))
                                    .addComponent(categorias, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addGap(9, 9, 9)
                        .addComponent(jButton1)
                        .addGap(11, 11, 11)
                        .addComponent(jLabel19)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel20)
                            .addComponent(preciocompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel21)
                            .addComponent(jLabel22))
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(precio_unitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel29)
                            .addComponent(jLabel28)
                            .addComponent(jLabel23)
                            .addComponent(jLabel32))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(precio_medio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel30)
                            .addComponent(cantidad_medio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel27)
                            .addComponent(jLabel24)
                            .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel33))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(precio_mayoreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel31)
                            .addComponent(cantidad_mayoreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel26)
                            .addComponent(jLabel25)
                            .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel34))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(precio_distribuidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel35)
                            .addComponent(cantidad_distribuidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16)
                            .addComponent(jLabel14)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15))
                        .addGap(0, 8, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(activo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(id_producto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(modelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(guardar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cantidad_mayoreoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantidad_mayoreoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cantidad_mayoreoActionPerformed

    private void precio_mayoreoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_precio_mayoreoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_precio_mayoreoActionPerformed

    private void precio_unitarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_precio_unitarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_precio_unitarioActionPerformed

    private void precio_medioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_precio_medioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_precio_medioActionPerformed

    private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed
        System.out.println(String.valueOf(tipo.getSelectedItem()));
        if (nombre.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Ingresa el nombre del producto");
        } else if (preciocompra.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Ingresa el precio de compra del producto");
        } else if (precio_unitario.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Ingresa el precio unitario del producto");
        } else if (cantidad_medio.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Ingresa la cantidad Medio Mayoreo del producto");
        } else if (cantidad_mayoreo.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Ingresa la cantidad de Mayoreo del producto");
        } else if (precio_medio.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Ingresa el precio de Medio Mayoreo del producto");
        } else if (precio_mayoreo.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Ingresa el precio de Mayoreo del producto");
        } else {
            try {
                guardarDatosProductos();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(editProducto.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


        // TODO add your handling code here:
    }//GEN-LAST:event_guardarActionPerformed

    private void guardarDatosProductos() throws FileNotFoundException {
        try {
            int valor = 0;
            valor = activo.getSelectedIndex();
            if (descripcion.getText().equals("")) {
                descripcion.setText("NULL");
            }
            if (especificaciones.getText().equals("")) {
                especificaciones.setText("NULL");
            }
            if (notas.getText().equals("")) {
                notas.setText("NULL");
            }
            if (modelo.getText().equals("")) {
                modelo.setText("NULL");
            }
            if (ubicacionalmacen.getText().equals("")) {
                ubicacionalmacen.setText("NULL");
            }
            if (cantidadalmacen.getText().equals("")) {
                cantidadalmacen.setText("0");
            }
            if (minimo_stock.getText().equals("")) {
                minimo_stock.setText("0");
            }
            if (peso.getText().equals("")) {
                peso.setText("0");
            }
            if (cantidad_medio.getText().equals("")) {
                cantidad_medio.setText("0");
            }
            if (cantidad_mayoreo.getText().equals("")) {
                cantidad_mayoreo.setText("0");
            }
            if (preciocompra.getText().equals("")) {
                preciocompra.setText("0");
            }
            if (precio_unitario.getText().equals("")) {
                precio_unitario.setText("0");
            }
            if (precio_mayoreo.getText().equals("")) {
                precio_mayoreo.setText("0");
            }
            if (precio_medio.getText().equals("")) {
                precio_medio.setText("0");
            }
            if(cantidad_stock.getText().equals("")){
                cantidad_stock.setText("0");
            }
            for (int i = 0; i < model2.getSize(); i++) {
                ps = (PreparedStatement) cn.prepareStatement("INSERT INTO categorias_productos VALUES ((select id_categoria from categorias where nombre = '" + model2.get(i) + "')," + codigo + ")");
                ps.execute();
            }
            ps = (PreparedStatement) cn.prepareStatement("insert into productos(activo,nombre,tipo,modelo,descripcion,especificaciones,notas,ubicacionalmacen,codigo_ubicacion,cantidadalmacen,cantidad_stock,minimo_stock,peso,preciocompra,precio_unitario,cantidad_medio,precio_medio,cantidad_mayoreo,precio_mayoreo,precio_distribuidor,cantidad_distribuidor) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1,activo.getSelectedIndex());
            ps.setString(2,nombre.getText());
            ps.setString(3,String.valueOf(tipo.getSelectedItem()));
            ps.setString(4, modelo.getText());
            ps.setString(5, descripcion.getText());
            ps.setString(6,especificaciones.getText());
            ps.setString(7, notas.getText());
            ps.setString(8,ubicacionalmacen.getText());
            ps.setString(9, codigo_ubicacion.getText());
            ps.setInt(10, Integer.parseInt(cantidadalmacen.getText()));
            ps.setInt(11, Integer.parseInt(cantidad_stock.getText()));
            ps.setInt(12,Integer.parseInt(minimo_stock.getText()));
            ps.setInt(13, Integer.parseInt(peso.getText()));
            ps.setFloat(14,Float.parseFloat(preciocompra.getText()));
            ps.setFloat(15,Float.parseFloat(precio_unitario.getText()));
            ps.setInt(16, Integer.parseInt(cantidad_medio.getText()));
            ps.setFloat(17, Float.parseFloat(precio_medio.getText()));
            ps.setInt(18, Integer.parseInt(cantidad_mayoreo.getText()));
            ps.setFloat(19,Float.parseFloat(precio_mayoreo.getText()));
            ps.setFloat(20,Float.parseFloat(precio_distribuidor.getText()));
            ps.setFloat(21,Float.parseFloat(cantidad_distribuidor.getText()));
            ps.executeUpdate();
            // Comprobamos que hay imagen
            if(hayImagen){
                ResultSet rs = ps.getGeneratedKeys();
                rs.next();
                int productId = rs.getInt(1);
                File file = fileChooser.getSelectedFile();
                FileInputStream fis = new FileInputStream(file);
                java.sql.PreparedStatement st;
                String ext = file.getName().split("\\.")[1];
                // Preparamos query para insertar el archivo
                ps = (PreparedStatement)cn.prepareStatement("INSERT INTO archivos(nombre,ext,archivo) VALUES(?,?,?)",Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, file.getName());  
                ps.setString(2, ext);                
                ps.setBinaryStream(3, fis, (int) file.length());
                ps.executeUpdate();
                // Hacemos relacion con el producto
                rs = ps.getGeneratedKeys();
                rs.next();
                ps = (PreparedStatement)cn.prepareStatement("INSERT INTO archivo_relacion(archivoId,productoId) VALUES(?,?)");
                ps.setInt(1,rs.getInt(1)); 
                ps.setInt(2, productId);
                ps.execute();
            }
            this.dispose();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

    }
    private void minimo_stockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minimo_stockActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_minimo_stockActionPerformed

    private void ubicacionalmacenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubicacionalmacenActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ubicacionalmacenActionPerformed

    private void preciocompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preciocompraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_preciocompraActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void preciocompraKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_preciocompraKeyPressed
// TODO add your handling code here:
    }//GEN-LAST:event_preciocompraKeyPressed

    private void preciocompraKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_preciocompraKeyReleased
        if (!preciocompra.getText().equals("")) {
            jLabel32.setText(String.valueOf(Float.parseFloat(preciocompra.getText()) + Float.parseFloat(preciocompra.getText()) * porcentajeSugerido() / 100) + '0');
        } else {
            jLabel32.setText("");
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_preciocompraKeyReleased

    private void jComboBox2PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBox2PopupMenuWillBecomeInvisible
        if (!preciocompra.getText().equals("")) {
            jLabel32.setText(String.valueOf(Float.parseFloat(preciocompra.getText()) + Float.parseFloat(preciocompra.getText()) * porcentajeSugerido() / 100) + '0');
        } else {
            jLabel32.setText("");
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2PopupMenuWillBecomeInvisible

    private void precio_unitarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_precio_unitarioKeyReleased
        sugerenciasMayoreos();
    }//GEN-LAST:event_precio_unitarioKeyReleased

    private void jComboBox3PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBox3PopupMenuWillBecomeInvisible
        sugerenciasMayoreos();
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox3PopupMenuWillBecomeInvisible

    private void jComboBox4PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBox4PopupMenuWillBecomeInvisible
        sugerenciasMayoreos();
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox4PopupMenuWillBecomeInvisible

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void activoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_activoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_activoActionPerformed

    private void tipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoActionPerformed
if (tipo.getSelectedIndex()!=2){    
             fecha.setEnabled(false);
}
else {
             fecha.setEnabled(true);   
}
        // TODO add your handling code here:
    }//GEN-LAST:event_tipoActionPerformed

    private void categoriasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_categoriasActionPerformed
Categorias categoria = new Categorias(this,true,codigo);
categoria.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_categoriasActionPerformed

    private void fechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fechaActionPerformed
cambiarFecha fechass = new cambiarFecha(this,true,codigo);
fechass.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_fechaActionPerformed

    private void precio_distribuidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_precio_distribuidorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_precio_distribuidorActionPerformed

    private void cantidad_distribuidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantidad_distribuidorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cantidad_distribuidorActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        sugerenciasMayoreos();
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void imageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imageButtonActionPerformed
        FileNameExtensionFilter filtro = new FileNameExtensionFilter(
        "JPG, PNG & GIF", "jpg", "gif", "jpeg", "png");
        fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
        fileChooser.setFileFilter(filtro);
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                Image img = ImageIO.read(fileChooser.getSelectedFile());
                Image resized = img.getScaledInstance(220, 112, 10);
                ImageIcon icon = new ImageIcon(resized);
                hayImagen=true;
                imageProduct.setIcon(icon);
            } catch (IOException ex) {
                Logger.getLogger(Almacen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_imageButtonActionPerformed
    void sugerenciasMayoreos() {
        if (!precio_unitario.getText().equals("")) {
            jLabel33.setText(String.valueOf(Float.parseFloat(precio_unitario.getText()) - Float.parseFloat(precio_unitario.getText()) * porcentajeMayoreos(jComboBox3.getSelectedIndex()) / 100) + '0');
            jLabel34.setText(String.valueOf(Float.parseFloat(precio_unitario.getText()) - Float.parseFloat(precio_unitario.getText()) * porcentajeMayoreos(jComboBox4.getSelectedIndex()) / 100) + '0');
            jLabel15.setText(String.valueOf(Float.parseFloat(precio_unitario.getText()) - Float.parseFloat(precio_unitario.getText()) * porcentajeMayoreos(jComboBox1.getSelectedIndex()) / 100) + '0');
        } else {
            jLabel33.setText("");
        }
    }

    float porcentajeMayoreos(int num) {
        switch (num) {
            case 1:
                return 10;
            case 2:
                return 15;
            case 3:
                return 20;
            case 4:
                return 25;
            case 5:
                return 30;
            default:
                return 5;
        }
    }

    float porcentajeSugerido() {
        switch (jComboBox2.getSelectedIndex()) {
            case 1:
                return 30;
            case 2:
                return 50;
            case 3:
                return 100;
            case 4:
                return 150;
            case 5:
                return 200;
            case 6:
                return 250;
            case 7:
                return 300;
            case 8:
                return 350;
            case 9:
                return 400;
            case 10:
                return 450;
            default:
                return 20;
        }
    }

    /**
     * @param args the command line arguments
     */
    class LimitadorCaracteres extends PlainDocument {

        /**
         * Método al que llama el editor cada vez que se intenta insertar
         * caracteres. Sólo debemos verificar arg1, que es la cadena que se
         * quiere insertar en el JTextField
         */
        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException {
            for (int i = 0; i < arg1.length(); i++) // si no es digit, volvemos
            {
                if (!Character.isDigit(arg1.charAt(i))) {
                    return;
                }
            }

            // Si todos son digit, insertamos el texto en el JTextField
            super.insertString(arg0, arg1, arg2);
        }
    }

    class LimitadorCaracteresFloat extends PlainDocument {

        /**
         * Método al que llama el editor cada vez que se intenta insertar
         * caracteres. Sólo debemos verificar arg1, que es la cadena que se
         * quiere insertar en el JTextField
         */
        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException {
            for (int i = 0; i < arg1.length(); i++) // si no es digit, volvemos
            {
                if (!(Character.isDigit(arg1.charAt(i)) || arg1.charAt(i) == '.')) {
                    return;
                }
            }

            // Si todos son digit, insertamos el texto en el JTextField
            super.insertString(arg0, arg1, arg2);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox activo;
    private javax.swing.JTextField cantidad_distribuidor;
    private javax.swing.JTextField cantidad_mayoreo;
    private javax.swing.JTextField cantidad_medio;
    private javax.swing.JTextField cantidad_stock;
    private javax.swing.JTextField cantidadalmacen;
    private javax.swing.JButton categorias;
    private javax.swing.JTextField codigo_ubicacion;
    private javax.swing.JTextArea descripcion;
    private javax.swing.JTextArea especificaciones;
    private javax.swing.JButton fecha;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JButton guardar;
    private javax.swing.JTextField id_producto;
    private javax.swing.JButton imageButton;
    private javax.swing.JLabel imageProduct;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField minimo_stock;
    private javax.swing.JTextField modelo;
    private javax.swing.JTextField nombre;
    private javax.swing.JTextArea notas;
    private javax.swing.JTextField peso;
    private javax.swing.JTextField precio_distribuidor;
    private javax.swing.JTextField precio_mayoreo;
    private javax.swing.JTextField precio_medio;
    private javax.swing.JTextField precio_unitario;
    private javax.swing.JTextField preciocompra;
    private javax.swing.JComboBox tipo;
    private javax.swing.JTextField ubicacionalmacen;
    // End of variables declaration//GEN-END:variables
}
